package cn.edu.lcu.cs.javaprogramming.io;

import cn.edu.lcu.cs.javaprogramming.BaseTest;
import lombok.Cleanup;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.stream.Stream;

/**
 * 读取网络资源示例
 *
 * @author ling
 * @date 2022/4/25 22:04
 */
class UrlReaderDemo extends BaseTest {

    /**
     * 读取网络文件
     *
     * @param urlString
     * @throws IOException
     */
    @ParameterizedTest
    @CsvSource({"http://ee-c.lcu.edu.cn/"})
    void readUrl(String urlString) throws IOException {
        URL url = new URL(urlString);
        try (Scanner scanner = new Scanner(url.openStream())) {
            String line = null;
            int lineNumber = 0;
            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                System.out.println(++lineNumber + "\t" + line);
            }
        }
    }

    /**
     * 读取网络文件
     *
     * @param urlString
     * @throws IOException
     */
    @ParameterizedTest
    @CsvSource({"http://ee-c.lcu.edu.cn/"})
    void readUrl2(String urlString) throws IOException {
        URL url = new URL(urlString);
        @Cleanup
        Scanner scanner = new Scanner(url.openStream());
        String line = null;
        int lineNumber = 0;
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            System.out.println(++lineNumber + "\t" + line);
        }
    }

}